#include "http/znx_http_args.h"
#include "comm/znx_log.h"


int main()
{
    znx_http_args_t     args;
    znx_http_arg_t      *arg;

    znx_pool_t          *pool = znx_pool_create();

    znx_http_args_init(&args);

    znx_str_t str = ZNX_STRING("a&&&b=&c&d=1&e=2&&f=3&h&=4&5");
    znx_http_args_parse(pool, &args, &str);

    znx_str_t dst_str = ZNX_NULL_STRING;
    znx_http_args_to_string(pool, &args, &dst_str);
    ZNX_DEF_LOG_DEBUG(NULL, "%V", &dst_str);

    if (str.len != dst_str.len || znx_strncmp(str.data, dst_str.data, str.len) != 0) {
        ZNX_DEF_LOG_FATAL(NULL, "expect %v, but got other", &str);
    }

    znx_str_t key = ZNX_STRING("z");
    znx_str_t val = ZNX_STRING("v");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_FALSE;
    znx_http_args_add(&args, arg);

    ZNX_STR_SET(&key, "");
    ZNX_STR_SET(&val, "aaa");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_FALSE;
    znx_http_args_add(&args, arg);

    ZNX_STR_SET(&key, "");
    ZNX_STR_SET(&val, "");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_FALSE;
    znx_http_args_add(&args, arg);

    ZNX_STR_SET(&key, "");
    ZNX_STR_SET(&val, "");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_TRUE;
    znx_http_args_add(&args, arg);

    ZNX_STR_SET(&key, "h");
    ZNX_STR_SET(&val, "");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_FALSE;
    znx_http_args_add(&args, arg);

    ZNX_STR_SET(&key, "i");
    ZNX_STR_SET(&val, "");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_TRUE;
    znx_http_args_add(&args, arg);

    ZNX_STR_SET(&key, "d");
    ZNX_STR_SET(&val, "2");
    arg = znx_pool_calloc(pool, sizeof(znx_http_arg_t));
    znx_http_arg_name_init(&arg->name, key.data, key.len);
    arg->val = val; arg->has_delimiter = ZNX_TRUE;
    znx_http_args_set(&args, arg);


    znx_http_args_to_string(pool, &args, &dst_str);
    ZNX_DEF_LOG_DEBUG(NULL, "%V", &dst_str);

    u_char *tmp = (u_char *)"a&&&b=&c&d=2&e=2&&f=3&h&=4&5&z=v&=aaa&&=&h&i=";
    size_t tmp_len = sizeof("a&&&b=&c&d=2&e=2&&f=3&h&=4&5&z=v&=aaa&&=&h&i=") - 1;

    if (dst_str.len != tmp_len || znx_strncmp(tmp, dst_str.data, tmp_len) != 0) {
        ZNX_DEF_LOG_FATAL(NULL, "expect %s, but got other", tmp);
    }

    znx_pool_destroy(pool);

    return 0;
}
